<template>
  <div>
    <el-dialog
      v-if="isDeit===true"
      title="修改规则"
      :visible.sync="addRuleDialog"
      width="40%"
      :before-close="dialogNone"
      class="dialog">
      <div class="content">
        <el-form :rules="reactedRules" :model="basicForm" ref="basicForm">
          <el-form-item label="安全类别" prop="safeTypeId">
            <el-select v-model="basicForm.safeTypeId" placeholder="请选择安全类别" clearable>
              <el-option
                v-for="item in type"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="题目数量" prop="quesNum">
            <el-input-number v-model="basicForm.quesNum" :min="1" label="请输入题目数量"></el-input-number>
          </el-form-item>
          <el-form-item label="单题分值" prop="singleScore">
            <el-input-number v-model="basicForm.singleScore" :min="1" label="请输入单题分值"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel('basicForm')">取 消</el-button>
        <el-button type="primary" @click="save('basicForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      v-else
      title="添加规则"
      :visible.sync="addRuleDialog"
      width="40%"
      :before-close="dialogNone"
      class="dialog">
      <div class="content">
        <el-form :rules="reactedRules" :model="basicForm" ref="basicForm">
          <el-form-item label="安全类别" prop="safeTypeId">
            <el-select v-model="basicForm.safeTypeId" placeholder="请选择安全类别" clearable>
              <el-option
                v-for="item in type"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="题目数量" prop="quesNum">
            <el-input-number v-model="basicForm.quesNum" :min="1" label="请输入题目数量"></el-input-number>
          </el-form-item>
          <el-form-item label="单题分值" prop="singleScore">
            <el-input-number v-model="basicForm.singleScore" :min="1" label="请输入单题分值"></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel('basicForm')">取 消</el-button>
        <el-button type="primary" @click="save('basicForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ExamMessage from '@/api/exam/examMessage'
export default {
  name: 'AddBigQuestion',
  inject:['reload'],
  data(){
    return {
      type: [],
      // basicForm: {
      //   safeTypeId: null,
      //   quesNum: 10,
      //   singleScore: 2,
      // },

      
      reactedRules: {
        safeTypeId: [
          { required: true, message: '请选择安全类别', trigger: 'blur' }
        ],
        quesNum: [
          { required: true, message: '请填写题目数量', trigger: 'change' }
        ],
        singleScore: [
          { required: true, message: '请填写单题分值', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    addRuleDialog: {
      type: Boolean
    },
    bigQuesId: {
      type: Number
    },
    examInfoId: {
      type: Number
    },
    quesType: {
      type: Number
    },
    id: {
      type: Number
    },
    isDeit: {
      type: Boolean,
      default: false
    },
    basicForm: {
      type: Object
    },
    currentScore: {
      type: Number
    },
    totalScoreB: {
      type: Number
    }
  },
  mounted(){
    this.getType()
  },
  methods: {
    dialogNone(){
      this.$emit("letDialogNone")
    },
    // 安全分类下拉列表
    getType(){
      ExamMessage.frontAll().then(res=>{
        this.type = res.data
      })
    },
    save(basicForm){
      console.log(this.bigQuesId,'this.ques')
      this.$refs[basicForm].validate((valid) => {
        if(valid){
          if(this.isDeit===true){
            let params = {
              id: this.id,
              bigQuesId: this.bigQuesId,
              quesNum: this.basicForm.quesNum,
              examInfoId: this.examInfoId,
              quesType: this.basicForm.quesType ,
              safeTypeId: this.basicForm.safeTypeId ,
              singleScore:  this.basicForm.singleScore
            }
            console.log(params,'更新规则')
            ExamMessage.updateConfig(params).then(res=>{
              if(res.code===0){
                this.$message({
                  type: 'success',
                  message: '更新成功'
                });
                this.$emit("letDialogNone")
                this.$refs[basicForm].resetFields();
                // 更新成功后调用获取所有数据的接口
                ExamMessage.eExamConfigGet(this.examInfoId).then(res=>{
                  if(res.code === 0){
                    this.$emit("updateQues",res.data)
                    console.log(res.data,'ques')
                  }
                })
              }else{
                this.$message({
                  type: 'success',
                  message: '更新失败'
                });
              }
            })
          }else{
            let score = this.currentScore + (this.basicForm.quesNum * this.basicForm.singleScore)
            console.log(score)
            if(score>this.totalScoreB){
              this.$message({
                type: 'error',
                message: '添加此配置后，试题总分值大于当前的试卷总分，请重新配置！'
              });
            }else{
              let params = {
                  bigQuesId: this.bigQuesId,
                  quesNum: this.basicForm.quesNum ,
                  examInfoId: this.examInfoId,
                  quesType: this.quesType ,
                  safeTypeId: this.basicForm.safeTypeId ,
                  singleScore:  this.basicForm.singleScore
                }
                console.log(params,'新增规则')
                ExamMessage.eExamConfig(params).then(res=>{
                  if(res.code===0){
                    console.log(res,'规则新增成功')
                    this.$message({
                      type: 'success',
                      message: '添加成功'
                    });
                    this.$emit("letDialogNone")
                    this.$refs[basicForm].resetFields();
                    // 添加成功后调用获取所有数据的接口
                    ExamMessage.eExamConfigGet(this.examInfoId).then(res=>{
                      if(res.code === 0){
                        this.$emit("updateQues",res.data)
                        console.log(res.data,'ques')
                      }
                    })
                  }
                })
              }
            }
        }else{
          return false
        }
      })
    },
    cancel(basicForm){
      this.$message({
        type: 'info',
        message: '已取消配置'
      });
      this.$emit("letDialogNone")
      this.$refs[basicForm].resetFields();
    }
    // childMethod(id){
    //   console.log(id)
    // }
  }
};
</script>

<style scoped>
  .content{
    margin: 0px 0 0px 20px;
  }
  div>>>.el-dialog{
    margin-top: 25vh !important;
  }
  div>>>.el-dialog__header{
    text-align: center;
  }
</style>